package org.jeecg.common.job.data.dao;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.datasource.jdbc.BaseDao;
import org.jeecg.common.datasource.jdbc.vo.PageInfo;
import org.jeecg.common.datasource.jdbc.vo.PageSort;
import org.jeecg.common.job.data.vo.JobLogVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class JobLogDao extends BaseDao {

    public void save(JobLogVo jobLog){
        this.insertIntoTable(jobLog);
    }

    public void delete(Date delteDate){
        String sql = "delete from sys_job_log where exec_begin_time > ?";
        this.jdbc.update(sql, delteDate);
    }

    /**
     * 分页查询日志
     *
     * @param pageSort 分页排序信息
     * @param content  关键字
     * @return
     */
    public PageInfo<JobLogVo> getJobLogs(PageSort pageSort, String content) {
        StringBuffer sql = new StringBuffer();
        sql.append("select lg.*, cfg.name jobName, cfg.bean_name beanName, cfg.bean_method beanMethod from sys_job_log lg ");
        sql.append("inner join sys_job_config cfg on lg.fk_job_id = cfg.id ");
        StringBuffer sreachSQL = new StringBuffer();
        List<Object> params = new ArrayList<>();
        if (StringUtils.isNotBlank(content)) {
            sreachSQL.append("and cfg.name like ? or cfg.bean_name like ? or cfg.bean_method like ? ");
            params.add("%" + content + "%");
            params.add("%" + content + "%");
            params.add("%" + content + "%");
        }
        if (StringUtils.isNotBlank(sreachSQL.toString())){
            sql.append("where").append(sreachSQL.toString().substring(3));
        }
        return this.findByPage(sql.toString(), params.toArray(new Object[0]), JobLogVo.class, pageSort);
    }

}
